package com.kye.express.business.networkpoint_query;

import android.arch.lifecycle.Lifecycle;
import android.content.Context;
import android.text.TextUtils;

import com.kye.addrpicker.model.AddressPickerResult;
import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.ApiService;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.networkpoint_query.RequestQueryWorkNet;
import com.kye.model.bean.networkpoint_query.ResultQueryWorkNet;
import com.kye.model.service.NetworkPointService;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface NetworkPointQueryActivityContract {

    interface NetworkPointQueryView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(ArrayList<ResultQueryWorkNet> datas);

        void onError(String msg);
    }

    class NetworkPointQueryViewPresenter extends BasePresenter<NetworkPointQueryView> {

        private Context mContext;

        public NetworkPointQueryViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 请求服务器数据
         */
        public void loadDatas(AddressPickerResult address) {
            RetrofitManager.getInstance().createOpenApiService(NetworkPointService.class)
                    .queryWorkNet(new RequestQueryWorkNet(address.getProvince(), address.getCity(), address.getArea(), address.getStreet()))
                    .compose(getView().getProvider().<OpenApiResult<ArrayList<ResultQueryWorkNet>>>bindUntilEvent(Lifecycle.Event.ON_DESTROY))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new OpenApiResultObserver<ArrayList<ResultQueryWorkNet>>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ArrayList<ResultQueryWorkNet>> result, ArrayList<ResultQueryWorkNet> datas) {
                            if (datas != null) {
                                getView().onLoaded(datas);
                            }else {
                                getView().onError(mContext.getString(R.string.common_no_datas));
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<ArrayList<ResultQueryWorkNet>> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onError(msg);
                            } else {
                                getView().onError(mContext.getString(R.string.common_net_error));
                            }
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }
                    }));
        }
    }
}
